<?php
/**
 * Index.php
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace app\agent\controller;

use addons\Nsfx\data\service\NfxCommissionQuery;
use data\service\Member\MemberAccount;
use data\service\Member as MemberService;

/**
 * 账户
 */
class Account extends BaseController
{
	public function account()
	{
        //账户类型
        $from_type_list = MemberAccount::getMemberAccountRecordsNameList();
        $from_type_need = [];
        foreach($from_type_list as $key=>$val){
            if(in_array('balance', $val['use_account_type']) || in_array('give_balance', $val['use_account_type'])){
                $from_type_need[] = $val;
            }
        }
        $this->assign('from_type_list', $from_type_need);

        //提现状态
        $member_service = new MemberService();
        $withdraw_status = $member_service->getWithdrawStatus();
        $this->assign('withdraw_status', $withdraw_status);

        //账户信息
        $account_info = $member_service->getMemberAccount($this->uid);
        $this->assign('account_info', $account_info);

		return view($this->style . 'Account/account');
	}

    /**
     * 分享奖励
     */
	public function commissionDistribution()
    {
        if(request()->isAjax())
        {
            $page_index = input('page_index', 1);
            $page_size = input('page_size', PAGESIZE);
            $start_time = request()->post('start_date', '');
            $end_time = request()->post('end_date', '');
            $is_issue = request()->post('is_issue', '0,1,2');

            $params = [];
            $params['uid'] = $this->uid;
            $params['start_time'] = $start_time;
            $params['end_time'] = $end_time;
            $params['is_issue'] = $is_issue;

            $commission_service = new NfxCommissionQuery();
            $res = $commission_service->getCommissionDistributionRecords($params, $page_index, $page_size);
            foreach($res['data'] as $key=>$val){
                if($val['is_issue'] == 0 || $val['is_issue'] == 1){
                    $issue_name = '待确认';
                }else{
                    $issue_name = '已确认';
                }
                $res['data'][$key]['issue_name'] = $issue_name;
            }
            return $res;
        }
    }

    /**
     * 服务奖励
     */
    public function commissionRegionAgent()
    {
        if(request()->isAjax())
        {

            $page_index = input('page_index', 1);
            $page_size = input('page_size', PAGESIZE);
            $start_time = request()->post('start_date', '');
            $end_time = request()->post('end_date', '');
            $is_issue = request()->post('is_issue', '0,1,2');

            $params = [];
            $params['uid'] = $this->uid;
            $params['start_time'] = $start_time;
            $params['end_time'] = $end_time;
            $params['is_issue'] = $is_issue;

            $commission_service = new NfxCommissionQuery();
            $res = $commission_service->getCommissionRegionAgentRecords($params, $page_index, $page_size);
            foreach($res['data'] as $key=>$val){
                if($val['is_issue'] == 0 || $val['is_issue'] == 1){
                    $issue_name = '待确认';
                }else{
                    $issue_name = '已确认';
                }
                $res['data'][$key]['issue_name'] = $issue_name;
            }
            return $res;
        }
    }

    /**
     * 提现记录
     */
    public function withdrawRecords()
    {
        if(request()->isAjax())
        {
            $page_index = request()->post('page_index', 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $start_time = request()->post('start_date', '');
            $end_time = request()->post('end_date', '');
            $withdraw_status = request()->post('withdraw_status', '');

            $condition['uid'] = $this->uid;
            $condition['shop_id'] = 0;
            if($start_time != '' && $end_time != ''){
                $condition['ask_for_date'] = ['between', [strtotime($start_time), strtotime($end_time)]];
            }else if($start_time != '' && $end_time == ''){
                $condition['ask_for_date'] = ['egt', strtotime($start_time)];
            }else if($start_time == '' && $end_time != ''){
                $condition['ask_for_date'] = ['elt', strtotime($end_time)];
            }
            if($withdraw_status != ''){
                $condition['status'] = $withdraw_status;
            }
            $order = 'ask_for_date desc';

            $member = new MemberService();
            $withdraw_list = $member->getMemberBalanceWithdraw($page_index, $page_size, $condition, $order);
            $status_arr = $member->getWithdrawStatus();
            foreach ($withdraw_list['data'] as $k => $v) {
                $withdraw_list['data'][ $k ]['status_name'] = $status_arr[$v['status']];
            }
            return $withdraw_list;
        }
    }

    /**
     * 账户记录
     */
    public function accountRecords()
    {
        if(request()->isAjax())
        {
            $account_type = request()->post('account_type', 1);
            $page_index = request()->post('page_index', 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $start_time = request()->post('start_date', '');
            $end_time = request()->post('end_date', '');
            $from_type = request()->post('from_type', 0);

            $condition['nmar.shop_id'] = 0;
            $condition['nmar.uid'] = $this->uid;
            $condition['nmar.account_type'] = ['in', $account_type];

            //发生时间
            if($start_time != '' && $end_time != ''){
                $condition['nmar.create_time'] = ['between', [strtotime($start_time), strtotime($end_time)]];
            }else if($start_time != '' && $end_time == ''){
                $condition['nmar.create_time'] = ['egt', strtotime($start_time)];
            }else if($start_time == '' && $end_time != ''){
                $condition['nmar.create_time'] = ['elt', strtotime($end_time)];
            }

            //来源类型
            if($from_type != 0){
                $condition['nmar.from_type'] = $from_type;
            }

            // 查看用户在该商铺下的消费流水
            $member = new MemberService();
            $account_records_list = $member->getAccountList($page_index, $page_size, $condition);
            return $account_records_list;
        }
    }
}